﻿using Microsoft.Win32;
using System.Diagnostics;

namespace 考试监视器
{
    internal class BrowserLauncher
    {
        public void OpenUrl(string url)
        {
            string chromePath = GetChromeInstallationPath();

            if (!string.IsNullOrEmpty(chromePath))
            {
                // 使用Google Chrome打开网址  
                Process.Start(new ProcessStartInfo(chromePath, url) { UseShellExecute = true });
            }
            else
            {
                try
                {
                    // 使用默认浏览器打开网址  
                    Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
                }
                catch (Exception ex)
                {
                    // 如果出现错误，记录错误但不抛出异常  
                    MessageBox.Show("请手动打开浏览器，输入 i.chaoxing.com  因为报错：" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private string GetChromeInstallationPath()
        {
            string chromePath = string.Empty;
            string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            string chromeRegistryPath = Path.Combine(localAppDataPath, "Google\\Chrome\\Application");

            if (Directory.Exists(chromeRegistryPath))
            {
                chromePath = Directory.GetFiles(chromeRegistryPath, "chrome.exe", SearchOption.TopDirectoryOnly)[0];
            }
            else
            {
                // 尝试从注册表中获取安装路径（32位和64位）  
                string[] registryPaths = new string[]
                {
                    @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
                    @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
                };

                foreach (string registryPath in registryPaths)
                {
                    using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registryPath))
                    {
                        if (key != null)
                        {
                            foreach (string subKeyName in key.GetSubKeyNames())
                            {
                                using (RegistryKey subKey = key.OpenSubKey(subKeyName))
                                {
                                    if (subKey != null)
                                    {
                                        string displayName = subKey.GetValue("DisplayName") as string;
                                        if (!string.IsNullOrEmpty(displayName) && displayName.Contains("Google Chrome"))
                                        {
                                            string installLocation = subKey.GetValue("InstallLocation") as string;
                                            if (!string.IsNullOrEmpty(installLocation))
                                            {
                                                chromePath = Path.Combine(installLocation, "chrome.exe");
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return chromePath;
        }
    }
}
